package com.yunda.AJDeviceService.controller;

import com.alibaba.fastjson.JSONObject;
import com.yunda.AJDeviceService.common.web.domain.ErlangsAjaxResult;
import com.yunda.AJDeviceService.data.dto.*;
import com.yunda.AJDeviceService.service.LDPSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author lq
 * @className LDPSController
 * @description TODO
 * @date 2023/2/10 09:28
 */
@Slf4j
@RestController
@RequestMapping
public class ErlangsLDPSController {


    @Autowired
    private LDPSService ldpsService;

    /**
     *
     * 请求方式：GET https
     * @return
     */
    @GetMapping("/service/rest/1.0/smterminal")
    public ErlangsAjaxResult zdTOLdps(@RequestParam Map<String,Object> inData) {

        if(ObjectUtils.isEmpty(inData)||ObjectUtils.isEmpty(inData.get("method"))){
            log.error("入参不能为空="+inData);
            return ErlangsAjaxResult.error(500,"入参不能为空");
        }
        //1.终端向LDPS注册  register
        //2.终端和LDPS保持心跳。一个周期 默认30s.在注册时返回心跳周期时间。周期时间LDPS上通过配置文件配置。3个周期失败作为离线标志 keepalive
        //3.终端向LDPS取消注册  unregister
        //4.终端向LDPS 获取KMS服务器信息 getkmsinfo
        //5.上传安检员信息 uploadinspector
        //6.上传安检机运行状态数据 uploadsmstatus
        //7.上传安检机采集的图片 uploadsmimage
        //8.上传安检机采集的违禁图片 uploadsmcontraband
        //9.上传安检机采集的违禁图片 uploadsmcontraband
        try {
            String method = (String) inData.get("method");
            ErlangsAjaxResult erlangsAjaxResult = new ErlangsAjaxResult();
            //1.终端向LDPS注册  register
            if(method.equals("register"))
            {
                //map转对象
                ZdRegisterTOLdpsDto zdRegisterTOLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZdRegisterTOLdpsDto.class);
                if(ObjectUtils.isEmpty(zdRegisterTOLdpsDto)||
                        ObjectUtils.isEmpty(zdRegisterTOLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zdRegisterTOLdpsDto.getId())){
                    log.error("入参不能为空="+zdRegisterTOLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Map<String,Object> result = ldpsService.zdRegisterTOLdps(zdRegisterTOLdpsDto);
                erlangsAjaxResult = ErlangsAjaxResult.success();
                erlangsAjaxResult.put("sessionid",result.get("sessionid"));
                erlangsAjaxResult.put("heartbeattime",result.get("heartbeattime"));
                return erlangsAjaxResult;
            }
            //2.终端和LDPS保持心跳。一个周期 默认30s.在注册时返回心跳周期时间。周期时间LDPS上通过配置文件配置。3个周期失败作为离线标志 keepalive
            else if(method.equals("keepalive"))
            {
                ZbBeatsTOLdpsDto zbBeatsTOLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZbBeatsTOLdpsDto.class);
                if(ObjectUtils.isEmpty(zbBeatsTOLdpsDto)||
                        ObjectUtils.isEmpty(zbBeatsTOLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zbBeatsTOLdpsDto.getSessionid())){
                    log.error("入参不能为空="+zbBeatsTOLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zdBeatsTOLdps(zbBeatsTOLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"fail");
                }
                return erlangsAjaxResult;
            }
            //3.终端向LDPS取消注册  unregister
            else if(method.equals("unregister"))
            {
                ZdUnRegisterTOLdpsDto zdUnRegisterTOLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZdUnRegisterTOLdpsDto.class);
                if(ObjectUtils.isEmpty(zdUnRegisterTOLdpsDto)||
                        ObjectUtils.isEmpty(zdUnRegisterTOLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zdUnRegisterTOLdpsDto.getSessionid())){
                    log.error("入参不能为空="+zdUnRegisterTOLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zdUnRegisterTOLdps(zdUnRegisterTOLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"fail");
                }
                return erlangsAjaxResult;
            }
            //4.终端向LDPS 获取KMS服务器信息 getkmsinfo
            else if(method.equals("getkmsinfo"))
            {
                ZdGetKmsInfoDto zdGetKmsInfoDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZdGetKmsInfoDto.class);
                if(ObjectUtils.isEmpty(zdGetKmsInfoDto)||
                        ObjectUtils.isEmpty(zdGetKmsInfoDto.getMethod())||
                        ObjectUtils.isEmpty(zdGetKmsInfoDto.getSessionid())||
                        ObjectUtils.isEmpty(zdGetKmsInfoDto.getHost())){
                    log.error("入参不能为空="+zdGetKmsInfoDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                String host = ldpsService.zdGetKmsInfoTOLdps(zdGetKmsInfoDto);
                if(!ObjectUtils.isEmpty(host)){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                    erlangsAjaxResult.put("a",host);
                    return erlangsAjaxResult;
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"终端未存活，不能获取KMS信息");
                    return erlangsAjaxResult;
                }
            }
            //5.上传安检员信息 uploadinspector
            else if(method.equals("uploadinspector"))
            {
                ZduploadinspectorToLdpsDto zduploadinspectorToLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZduploadinspectorToLdpsDto.class);
                if(ObjectUtils.isEmpty(zduploadinspectorToLdpsDto)||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getSessionid())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getName())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getIndex())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getSex())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getBirth())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getCardno())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getCompany())||
                        ObjectUtils.isEmpty(zduploadinspectorToLdpsDto.getCertificateno())
                ){
                    log.error("入参不能为空="+zduploadinspectorToLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zduploadinspectorTOLdps(zduploadinspectorToLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                    return erlangsAjaxResult;
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"终端未存活，不能上传安检员信息信息");
                    return erlangsAjaxResult;
                }
            }
            //6.上传安检员信息 uploadinspector
            else if(method.equals("uploadsmstatus"))
            {
                ZbuploadsmstatusToLdpsDto zbuploadsmstatusToLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZbuploadsmstatusToLdpsDto.class);
                if(ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto)||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getSessionid())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getMachinetype())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getMachineindex())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getRaytype())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getVoltage())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getCurrent())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getScannertype())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getPackagecount())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getXrayconnected())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getScannerconnected())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getTemperature())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getHumidity())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getLastpoweron())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getLastpoweroff())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getUsr_dura_pack())||
                        ObjectUtils.isEmpty(zbuploadsmstatusToLdpsDto.getPackagecount())
                ){
                    log.error("入参不能为空="+zbuploadsmstatusToLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zduploadsmstatusTOLdps(zbuploadsmstatusToLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                    return erlangsAjaxResult;
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"终端未存活，不能上传安检员信息信息");
                    return erlangsAjaxResult;
                }
            }
            //7.上传安检机采集的图片: uploadsmimage
            else if(method.equals("uploadsmimage"))
            {
                ZduploadsmimageToLdpsDto zduploadsmimageToLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZduploadsmimageToLdpsDto.class);
                if(ObjectUtils.isEmpty(zduploadsmimageToLdpsDto)||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getSessionid())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getImageurl())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getThumbnailurl())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getScantime())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getScanindex())
                ){
                    log.error("入参不能为空="+zduploadsmimageToLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zduploadsmimageTOLdps(zduploadsmimageToLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                    return erlangsAjaxResult;
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"终端未存活，不能上传安检机采集的图片");
                    return erlangsAjaxResult;
                }
            }
            //8.上传安检机采集的违禁图片: uploadsmcontraband
            else if(method.equals("uploadsmcontraband"))
            {
                ZduploadsmimageToLdpsDto zduploadsmimageToLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZduploadsmimageToLdpsDto.class);
                if(ObjectUtils.isEmpty(zduploadsmimageToLdpsDto)||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getSessionid())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getImageurl())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getThumbnailurl())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getScantime())||
                        ObjectUtils.isEmpty(zduploadsmimageToLdpsDto.getScanindex())
                ){
                    log.error("入参不能为空="+zduploadsmimageToLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                Boolean boo = ldpsService.zduploadsmimageTOLdps(zduploadsmimageToLdpsDto);
                if(boo){
                    erlangsAjaxResult = ErlangsAjaxResult.success();
                    return erlangsAjaxResult;
                }else{
                    erlangsAjaxResult = ErlangsAjaxResult.error(500,"终端未存活，不能上传安检机采集的图片");
                    return erlangsAjaxResult;
                }
            }
            //9.上传安检机采集的违禁图片 uploadsmcontraband
            else if(method.equals("uploadsmcontraband"))
            {
                ZduploadsmcontrabandToLdpsDto zduploadsmcontrabandToLdpsDto = JSONObject.parseObject(JSONObject.toJSONString(inData), ZduploadsmcontrabandToLdpsDto.class);
                if(ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto)||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getMethod())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getSessionid())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getContraband_type())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getContraband_num())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getScantime())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getScanindex())||
                        ObjectUtils.isEmpty(zduploadsmcontrabandToLdpsDto.getDownload_url1())
                ){
                    log.error("入参不能为空="+zduploadsmcontrabandToLdpsDto);
                    return ErlangsAjaxResult.error(500,"入参不能为空");
                }
                erlangsAjaxResult = ldpsService.zduploadsmcontrabandTOLdps(zduploadsmcontrabandToLdpsDto);
                return erlangsAjaxResult;
            }
            else
            {
                return ErlangsAjaxResult.error(500,"无此方法，请求无效");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ErlangsAjaxResult.error(500,e.toString());
        }
    }
}
